package com.changpay.zywy.ui.activity.mine

import android.content.Intent
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.app.App
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.constant.Constant
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.utils.Preference
import com.ruffian.library.widget.RCheckBox
import kotlinx.android.synthetic.main.activity_pay_password_change.*
import kotlinx.android.synthetic.main.title_layout.*
import java.lang.StringBuilder
import java.util.HashMap

class PayPasswordChangeActivity : BaseActivity(), IView {

    var stringBuilder: StringBuilder = StringBuilder()
    var index: Int = 0
    var imp_type: Int = 0
    val checkBoxList = arrayListOf<RCheckBox>()
    /**
     * local username
     */
    var user: String by Preference(Constant.USERNAME_KEY, "")


    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
        val app = App.instance as App
        app.finishTopActivity(2)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
            R.id.number_one -> takePlay("1")
            R.id.number_two -> takePlay("2")
            R.id.number_three -> takePlay("3")
            R.id.number_four -> takePlay("4")
            R.id.number_five -> takePlay("5")
            R.id.number_six -> takePlay("6")
            R.id.number_seven -> takePlay("7")
            R.id.number_eight -> takePlay("8")
            R.id.number_nine -> takePlay("9")
            R.id.number_zero -> takePlay("0")
            R.id.number_sure -> takePlay("sure")
            R.id.number_back -> takePlay("back")
//            R.id.setting_one-> startActivity(ChangePhonePwActivity::class.java)
//            R.id.setting_two -> Intent(this, GetCodeActivity::class.java).run {
//                putExtra("captcha_type", "7")
//                putExtra("title", "请先通过短信验证")
//                startActivity(this)
//            }
        }
    }


    override fun attachLayoutRes(): Int {
        return R.layout.activity_pay_password_change
    }

    override fun initData() {

    }

    override fun initView() {
        tv_title.text = intent.getStringExtra("activity_title")
        iv_back.setOnClickListener(onClickListener)
        pay_password_title.text = intent.getStringExtra("title")
        pay_password_message.text = intent.getStringExtra("message")
        imp_type = intent.getIntExtra("imp_type", 0)
        number_one.setOnClickListener(onClickListener)
        number_two.setOnClickListener(onClickListener)
        number_three.setOnClickListener(onClickListener)
        number_four.setOnClickListener(onClickListener)
        number_five.setOnClickListener(onClickListener)
        number_six.setOnClickListener(onClickListener)
        number_seven.setOnClickListener(onClickListener)
        number_eight.setOnClickListener(onClickListener)
        number_nine.setOnClickListener(onClickListener)
        number_zero.setOnClickListener(onClickListener)
        number_sure.setOnClickListener(onClickListener)
        number_back.setOnClickListener(onClickListener)
        checkBoxList.add(cb_one)
        checkBoxList.add(cb_two)
        checkBoxList.add(cb_three)
        checkBoxList.add(cb_four)
        checkBoxList.add(cb_five)
        checkBoxList.add(cb_six)
    }

    override fun start() {

    }


    private fun takePlay(type: String) {
        when (type) {
            "back" -> {
                if (stringBuilder.isEmpty()) return
                stringBuilder.delete(stringBuilder.length - 1, stringBuilder.length)
                checkBoxList[stringBuilder.length].isChecked = false
            }
            "sure" -> {
                if (stringBuilder.length < 6) {
                    showToast("长度不足6位")
                    return
                }
                sure(stringBuilder.toString())
            }
            else -> {
                if (stringBuilder.length >= 6) return
                stringBuilder.append(type)
                checkBoxList[stringBuilder.length - 1].isChecked = true
            }
        }

    }

    private fun sure(password: String) {
        when (imp_type) {
            0 -> {
                val intent = Intent(this, PayPasswordChangeActivity::class.java)
                intent.putExtra("activity_title", "修改支付密码")
                intent.putExtra("title", "确认新支付密码")
                intent.putExtra("message", "第二步确认新支付密码")
                intent.putExtra("imp_type", 1)
                intent.putExtra("before_password", password)
                intent.putExtra("captcha", getIntent().getStringExtra("captcha"))
                startActivity(intent)
            }

            1 -> {
// 第二部去获取前一个页面的数据
                val before_password = intent.getStringExtra("before_password")
                if (before_password == password) {
                    val params = HashMap<String, String>()
//                    params["captcha"] = intent.getStringExtra("captcha")
                    params["phone"] = user
                    params["paypwd"] = password
                    params["key"] = token
                    RetrofitHelper.service.update_paypwd(params).sss(this)
                    {
                        showToast(it.result.toString())
                        val app = App.instance as App
                        app.finishTopActivity(3)
                    }
                } else {
                    showToast("两次密码输入不一致,请重新输入")

                }
            }
        }

    }

}
